package com.test21_常用类;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

/**
 * Random，随机数类
 */
public class Demo08_Random {

    @Test
    public void test1() {
        Random random = new Random();
        for (int x = 0; x < 10; x++) {
            System.out.println(random.nextInt(100));
        }
    }

    @Test
    public void test2() {
        LotteryTicket lotteryTicket = new LotteryTicket();
        lotteryTicket.random();
        for (int temp : lotteryTicket.getData()) {
            System.out.println(temp);
        }
    }
}

/**
 * 彩票工具类
 */
class LotteryTicket {
    private int index;      // 手工控制生产索引
    private int[] data;     // 保存最终生成的彩票数据
    private Random rand = new Random();

    public LotteryTicket() {
        this.data = new int[7];
    }

    public void random() {  // 随机生成彩票数据
        while (this.index < this.data.length) {
            int code = this.rand.nextInt(37);
            if (this.isExists(code)) {
                this.data[this.index++] = code;
            }
        }
    }

    private boolean isExists(int code) {
        if (code == 0) {
            return false;
        }
        for (int temp : this.data) {
            if (temp == code) {
                return false;
            }
        }
        return true;
    }

    public int[] getData() {
        Arrays.sort(this.data);
        return this.data;
    }
}
